package com.cn.yugan.Utils.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.cn.yugan.mobile.R;
import com.cn.yugan.Utils.DensityUtil;

/**
 * Created by Rover on 2016/7/20 .
 * Email 357319517@qq.com
 */

public class CustomDialog extends Dialog {
    public CustomDialog(Context context) {
        super(context);
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private String edithint;
        private String radiogroup;
        private String positiveButtonText;
        private String negativeButtonText;
        private String htmlinfo;
        
        private View contentView;
        private DialogInterface.OnClickListener positiveButtonClickListener;
        private DialogInterface.OnClickListener negativeButtonClickListener;
        private TextView textView;
        private EditText editText;
        private RadioGroup radioGroup;
        private WebView webView;
        private String radiogroupstr;
        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * 设置消息
         * Message的作用：1.生成tv 2.设置TV的文字信息
         *
         * @param
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        /**
         * 设置EditText的Hint
         * 作用：1.生成edit 2.设置hint
         */
        public Builder setEditHint(String editHint) {
            this.edithint = editHint;
            return this;
        }

        /**
         * 设置radiogroup
         */
        public Builder setRadioGroup(String radiogroup) {
            this.radiogroup = radiogroup;
            return this;
        }
        /**
         * 设置EditText的Hint
         * 作用：1.生成edit 2.设置hint
         */
        public Builder setWebview(String htmlinfo) {
            this.htmlinfo = htmlinfo;
            return this;
        }

        /**
         * 从资源文件中获取标题
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * 从字符串获取标题
         *
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * 设置view
         *
         * @param v
         * @return
         */
        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }


        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public TextView getTextview() {

            return textView;
        }

        public EditText getEditText() {
            return editText;

        }
        
        public String getRadioGroupinfo(){
            return radiogroupstr;
        }

        public CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme  
            final CustomDialog dialog = new CustomDialog(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_normal_layout, null);
            dialog.addContentView(layout, new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            // set the dialog title  
            ((TextView) layout.findViewById(R.id.dialog_title)).setText(title);
            // set the confirm button  
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.dialog_ok_bt))
                        .setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.dialog_ok_bt))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE  
                layout.findViewById(R.id.dialog_ok_bt).setVisibility(
                        View.GONE);
            }
            // set the cancel button  
            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.dialog_cancel_bt))
                        .setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.dialog_cancel_bt))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    negativeButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE  
                layout.findViewById(R.id.dialog_cancel_bt).setVisibility(
                        View.GONE);
            }
            if (message != null) {

                textView = (TextView) inflater.inflate(R.layout.dialog_textview, null);
                textView.setText(message);
                LinearLayout linearLayout = (LinearLayout) layout.findViewById(R.id.content);
                linearLayout.addView(textView);
            } else if (edithint != null) {
                editText = (EditText) inflater.inflate(R.layout.dialog_edittext, null);
                editText.setText(edithint);
                LinearLayout linearLayout = (LinearLayout) layout.findViewById(R.id.content);
                linearLayout.addView(editText);

            }else if (htmlinfo!=null){
                webView = (WebView) inflater.inflate(R.layout.dialog_webview, null);
                webView.setWebChromeClient(new WebChromeClient());
                webView.loadUrl(htmlinfo);
                webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                LinearLayout linearLayout = (LinearLayout) layout.findViewById(R.id.content);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, DensityUtil.dip2px(context,300));
                linearLayout.setLayoutParams(layoutParams);
                linearLayout.addView(webView);
            }else if (radiogroup != null) {
                if (radiogroup.equals("男") || radiogroup.equals("女")) {
                    radioGroup = (RadioGroup) inflater.inflate(R.layout.dialog_radiogroup_sex, null);
                    if (radiogroup.equals("男")) {
                        radioGroup.check(R.id.radioMale);
                    } else if (radiogroup.equals("女")) {
                        radioGroup.check(R.id.radioFemale);
                    }
                    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
                            radiogroupstr = radioButton.getText().toString();
                        }
                    });
                    LinearLayout linearLayout = (LinearLayout) layout.findViewById(R.id.content);
                    linearLayout.addView(radioGroup);
                }else if (radiogroup.equals("仅移动网络环境无图")||radiogroup.equals("仅WIFI网络环境无图")||radiogroup.equals("全局无图")){
                    radioGroup = (RadioGroup) inflater.inflate(R.layout.dialog_radiogroup_noneimg, null);
                    if (radiogroup.equals("仅移动网络环境无图")) {
                        radioGroup.check(R.id.radio_noimg_gprs);
                    } else if (radiogroup.equals("仅WIFI网络环境无图")) {
                        radioGroup.check(R.id.radio_noimg_wifi);
                    }else {
                        radioGroup.check(R.id.radio_noimg_all);
                    }
                    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
                            radiogroupstr = radioButton.getText().toString();
                        }
                    });
                    LinearLayout linearLayout = (LinearLayout) layout.findViewById(R.id.content);
                    linearLayout.addView(radioGroup);
                }
            } else if (contentView != null) {
                // if no message set  
                // add the contentView to the dialog body  
                ((LinearLayout) layout.findViewById(R.id.content))
                        .removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content))
                        .addView(contentView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            }
            dialog.setContentView(layout);
            return dialog;
        }
    }
}
